package org.kernely.stream.userpage;

import org.kernely.stream.StreamBundle;
import org.kernely.ui.mvp.IKernelyView;
import org.kernely.ui.mvp.KernelyEventSender;

import com.vaadin.terminal.ExternalResource;
import com.vaadin.terminal.Sizeable;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.ProgressIndicator;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.AbstractTextField.TextChangeEventMode;
import com.vaadin.ui.themes.Reindeer;

public class StreamMessagesView extends GridLayout implements IKernelyView, KernelyEventSender{
	
	/**
	 * Serial version ID
	 */
	private static final long serialVersionUID = -6728075455240022394L;

	private Panel messagesPanel;
	private Panel detailsPanel;
	private Panel messageMainContent;
	private Panel postPanel;
	private HorizontalLayout postLayout;
	private Panel displayPanel;
	private Button streamSettingsButton;
	private TextField inputMessage;
	private HorizontalLayout buttonsLayout;
	private Label lengthLabel;
	private Label infoLabel;
	private Button postButton;
	private VerticalLayout displayLayout;
	private Button moreResults;
	private VerticalLayout moreResultLayout;
	private VerticalLayout newMessagesLayout;
	private VerticalLayout allMessageLayout;
	private VerticalLayout detailsLayout;
	private ComboBox streamsBox;
	private Label streamName;
	private Label messageContent;
	private Panel commentPanel;
	private VerticalLayout commentLayout;
	
	private Panel postCommentPanel;
	private TextField inputComment;
	private Button postCommentButton;
	private Label infoCommentLabel;
	private HorizontalLayout buttonsCommentLayout;
	private Label lengthCommentLabel;
	private Label messageDisplayed;
	
	
	/**
	 * StreamUserPageView constructor
	 */
	public StreamMessagesView(){
		streamsBox = new ComboBox();
		streamsBox.setNullSelectionAllowed(false);
		
		// Artisanal pool system.
		ProgressIndicator poolMessages = new ProgressIndicator();
		poolMessages.setIndeterminate(true);
		poolMessages.setEnabled(true);
		poolMessages.setVisible(true);
		poolMessages.setPollingInterval(5000);
		
		messagesPanel = new Panel();
		detailsPanel = new Panel();
		messageMainContent = new Panel();
		messageMainContent.setStyleName(Panel.STYLE_LIGHT);
		displayPanel = new Panel();
		displayPanel.setSizeFull();
		displayPanel.setHeight(500, Sizeable.UNITS_PIXELS);
		displayPanel.setScrollable(true);
		displayLayout = new VerticalLayout();
		displayLayout.setSpacing(true);
		displayLayout.setMargin(true);
		displayPanel.setContent(displayLayout);
		allMessageLayout = new VerticalLayout();
		allMessageLayout.setSizeFull();
		allMessageLayout.setImmediate(true);
		
		postPanel = new Panel();
		postPanel.setSizeFull();
		inputMessage = new TextField();
		inputMessage.setSizeFull();
		inputMessage.setHeight(25, Sizeable.UNITS_PIXELS);
		inputMessage.setTextChangeEventMode(TextChangeEventMode.LAZY);
		inputMessage.setTextChangeTimeout(50);
		inputMessage.setImmediate(true);
		buttonsLayout = new HorizontalLayout();
		buttonsLayout.setSizeFull();
		lengthLabel = new Label();
		lengthLabel.setImmediate(true);
		
		infoLabel = new Label();
		infoLabel.setContentMode(Label.CONTENT_XHTML);
		infoLabel.setImmediate(true);
		
		postButton = new Button("Post");
		postButton.setImmediate(true);
		postButton.setEnabled(false);
		streamSettingsButton = new Button();
		streamSettingsButton.setIcon(new ExternalResource("/streambundle/images/custom.png"));
		
		VerticalLayout vlayout = new VerticalLayout();
		
		moreResults = new Button("More results");
		moreResultLayout = new VerticalLayout();
		moreResultLayout.setSizeFull();
		moreResultLayout.addComponent(moreResults);
		moreResultLayout.setComponentAlignment(moreResults, Alignment.MIDDLE_CENTER);
		
		postPanel.addComponent(inputMessage);
		postPanel.addComponent(buttonsLayout);

		postLayout = new HorizontalLayout();
		postLayout.setSizeUndefined();
		postLayout.setSpacing(true);
		postLayout.addComponent(streamsBox);
		postLayout.addComponent(postButton);
		
		buttonsLayout.addComponent(lengthLabel);
		buttonsLayout.addComponent(infoLabel);
		buttonsLayout.addComponent(postLayout);
		
		buttonsLayout.setComponentAlignment(lengthLabel, Alignment.MIDDLE_CENTER);
		buttonsLayout.setComponentAlignment(postLayout, Alignment.MIDDLE_RIGHT);
		buttonsLayout.setMargin(true, false, false, false);
		
		newMessagesLayout = new VerticalLayout();
		newMessagesLayout.setSizeFull();
		newMessagesLayout.setImmediate(true);
		
		messageMainContent.setSizeFull();
		messageMainContent.setContent(new VerticalLayout());
		messageMainContent.addComponent(postPanel);
		messageMainContent.addComponent(displayPanel);
		
		displayPanel.addComponent(newMessagesLayout);
		displayPanel.addComponent(allMessageLayout);
		displayPanel.addComponent(moreResultLayout);

		streamName = new Label("Details");
		streamName.setStyleName(Reindeer.LABEL_H2);
		streamName.setSizeUndefined();
		messageContent = new Label("Click on a message to see its details.");
		messageContent.setContentMode(Label.CONTENT_XHTML);
		commentPanel = new Panel();
		commentPanel.setSizeFull();
		commentPanel.setHeight(400, Sizeable.UNITS_PIXELS);
		commentPanel.setStyle(Panel.STYLE_LIGHT);
		commentPanel.setScrollable(true);
		commentPanel.setImmediate(true);
		commentLayout = new VerticalLayout();
		commentLayout.setImmediate(true);
		commentPanel.setContent(commentLayout);
		detailsLayout = new VerticalLayout();
		detailsLayout.setSizeFull();
		detailsLayout.setSpacing(true);
		detailsLayout.setMargin(true);
		detailsPanel.setContent(detailsLayout);
		detailsPanel.addComponent(streamName);
		detailsPanel.addComponent(messageContent);
		detailsPanel.addComponent(commentPanel);
		detailsLayout.setComponentAlignment(streamName, Alignment.MIDDLE_CENTER);
		
		VerticalLayout messagesLayout = new VerticalLayout();
		messagesPanel.setContent(messagesLayout);
		HorizontalLayout buttonsLayout = new HorizontalLayout();
		buttonsLayout.addComponent(streamSettingsButton);
		buttonsLayout.setSizeUndefined();
		messagesLayout.addComponent(buttonsLayout);
		messagesLayout.setComponentAlignment(buttonsLayout, Alignment.MIDDLE_RIGHT);
		messagesLayout.addComponent(messageMainContent);
		messagesLayout.setSizeFull();
		messagesLayout.setMargin(true);
		messagesLayout.setSpacing(true);
		
		
		postCommentPanel = new Panel();
		
		inputComment = new TextField();
		inputComment.setSizeFull();
		inputComment.setHeight(25, Sizeable.UNITS_PIXELS);
		inputComment.setTextChangeEventMode(TextChangeEventMode.LAZY);
		inputComment.setTextChangeTimeout(50);
		inputComment.setImmediate(true);
		buttonsCommentLayout = new HorizontalLayout();
		buttonsCommentLayout.setSizeFull();
		lengthCommentLabel = new Label();
		lengthCommentLabel.setImmediate(true);
		
		infoCommentLabel = new Label();
		infoCommentLabel.setContentMode(Label.CONTENT_XHTML);
		infoCommentLabel.setImmediate(true);
		
		postCommentButton = new Button("Comment");
		postCommentButton.setImmediate(true);
		postCommentButton.setEnabled(false);
		
		postCommentPanel.addComponent(inputComment);
		postCommentPanel.addComponent(buttonsCommentLayout);
		
		buttonsCommentLayout.addComponent(lengthCommentLabel);
		buttonsCommentLayout.addComponent(infoCommentLabel);
		buttonsCommentLayout.addComponent(postCommentButton);
		
		buttonsCommentLayout.setComponentAlignment(lengthCommentLabel, Alignment.MIDDLE_CENTER);
		buttonsCommentLayout.setComponentAlignment(postCommentButton, Alignment.MIDDLE_RIGHT);
		buttonsCommentLayout.setMargin(true, false, false, false);
		
		messageDisplayed = new Label();
		messageDisplayed.setVisible(false);
		
		postCommentPanel.setEnabled(false);
		postCommentPanel.addComponent(messageDisplayed);
		detailsPanel.addComponent(postCommentPanel);
		detailsPanel.setHeight(680, Sizeable.UNITS_PIXELS);
		
		
		inputMessage.focus();
		
		this.setRows(1);
		this.setColumns(5);
		this.addComponent(messagesPanel, 0, 0, 2, 0);
		this.addComponent(detailsPanel, 3, 0, 4, 0);
		
		HorizontalLayout loadLayout = new HorizontalLayout();
		loadLayout.addComponent(poolMessages);
		loadLayout.setStyleName("invisible");
		
		this.addComponent(loadLayout);
		
		this.setSizeFull();
	}
	
	/**
	 * @return the messageDisplayed
	 */
	public Label getMessageDisplayed() {
		return messageDisplayed;
	}

	/**
	 * @return the postCommentPanel
	 */
	public Panel getPostCommentPanel() {
		return postCommentPanel;
	}

	/**
	 * @return the inputComment
	 */
	public TextField getInputComment() {
		return inputComment;
	}

	/**
	 * @return the postCommentButton
	 */
	public Button getPostCommentButton() {
		return postCommentButton;
	}

	/**
	 * @return the infoCommentLabel
	 */
	public Label getInfoCommentLabel() {
		return infoCommentLabel;
	}

	/**
	 * @return the lengthCommentLabel
	 */
	public Label getLengthCommentLabel() {
		return lengthCommentLabel;
	}

	/**
	 * @return the commentPanel
	 */
	public Panel getCommentPanel() {
		return commentPanel;
	}

	/**
	 * @return the commentLayout
	 */
	public VerticalLayout getCommentLayout() {
		return commentLayout;
	}

	/**
	 * @return the streamName
	 */
	public Label getStreamName() {
		return streamName;
	}

	/**
	 * @return the messageContent
	 */
	public Label getMessageContent() {
		return messageContent;
	}

	/**
	 * @return the allMessageLayout
	 */
	public VerticalLayout getAllMessageLayout() {
		return allMessageLayout;
	}

	/**
	 * @return the moreResultLayout
	 */
	public VerticalLayout getMoreResultLayout() {
		return moreResultLayout;
	}

	/**
	 * @return the moreResults
	 */
	public Button getMoreResults() {
		return moreResults;
	}

	/**
	 * @return the newMessagesLayout
	 */
	public VerticalLayout getNewMessagesLayout() {
		return newMessagesLayout;
	}

	/**
	 * @return the infoLabel
	 */
	public Label getInfoLabel() {
		return infoLabel;
	}

	/**
	 * @return the inputMessage
	 */
	public TextField getInputMessage() {
		return inputMessage;
	}

	/**
	 * @return the lengthLabel
	 */
	public Label getLengthLabel() {
		return lengthLabel;
	}

	/**
	 * @return the postButton
	 */
	public Button getPostButton() {
		return postButton;
	}

	/**
	 * Useless method.
	 */
	public Component getComponent() {
		return null;
	}

	/**
	 * Gets the streamSettingsButton
	 * @return the streamSettingsButton
	 */
	public Button getStreamSettingsButton() {
		return streamSettingsButton;
	}

	/**
	 * This method is called when this component is added to an other.
	 */
	@Override
	public void attach(){
		super.attach();
		//When the component is attached, we fire listeners
		StreamBundle.getEventBus().broadcastEvent(new StreamMustDisplayMessagesEvent(this));
	}

	/**
	 * Gets the displayLayout, where messages are displayed.
	 * @return the displayLayout
	 */
	public VerticalLayout getDisplayLayout() {
		return displayLayout;
	}

	/**
	 * Gets the streamsBox
	 * @return the streamsBox
	 */
	public ComboBox getStreamsBox() {
		return streamsBox;
	}
}
